using System;
using System.Collections;
using Gtk;
using System.Xml.XPath;

namespace doan_rss_02
{
	public class Subscription
	{
		public Subscription ()
		{
			articles = new ArrayList ();
		}

		public string name;
		public string source_url;
		public ArrayList articles;

		int next_unread ()
		{
			int i = 0;
			foreach (Article ar in articles) {
				if (ar.read == false) {
					return i;
				}
				i++;
			}
			return 0;
		}

		public int unread_count ()
		{
			int count = 0;
			foreach (Article ar in articles) {
				if (ar.read == false) {
					count++;
				}
			}
			return count;
		}

		bool is_cached (Article ar)
		{
			foreach (Article cached in articles) {
				if (ar.headline == cached.headline && ar.date == cached.date) {
					return true;
				}
			}
			return false;
		}

		public bool has_unread ()
		{
			foreach (Article ar in articles) {
				if (ar.read == false)
					return true;
			}
			return false;
		}

		public int subscribe (string s_url)
		{
			RssReader reader = new RssReader ();
			reader.RdfMode = false; /* I don't know what this is */
			// reader.FeedLoaded += new EventHandler(reader_feedloaded);
			reader.ItemAdded += new EventHandler (reader_ItemAdded);
			//reader.Error += new RssReaderErrorEventHandler
			RssFeed feed = reader.Retrieve (s_url);

			name = feed.Title;
			source_url = s_url;

			Article this_article;
			foreach (RssItem item in feed.Items) {
				this_article = new Article ();
				this_article.article_content = item.Description;
				this_article.author = item.Author; /* there is another tag dc:creator, RSS is a mess */
				this_article.date = item.Pubdate;
				Console.WriteLine("Pubdate: " + item.Guid);
				this_article.headline = item.Title;
				this_article.read = false;
				this_article.score = 0;
				articles.Add (this_article);
			}
//			/* http://news.mono-project.com/feed/ */
//			XPathDocument rss_file = new XPathDocument (s_url);
//
//			//source_url = s_url;
//
//			XPathNavigator xNav = rss_file.CreateNavigator ();
//
//			xNav.MoveToRoot ();
//			Console.WriteLine ("At root " + xNav.Name);
//
//			xNav.MoveToFirstChild (); /* move to rss */
//			Console.WriteLine ("At rss " + xNav.Name);
//			xNav.MoveToFirstChild (); /* move to channel */
//
//			Console.WriteLine ("At chanel " + xNav.Name);
//			xNav.MoveToFirstChild (); /* at title  */
//
//			Console.WriteLine ("At title" + xNav.Name);
//
//			name = xNav.Value;
//
//			while (xNav.Name != "item") {
//				xNav.MoveToNext ();
//			}
//
//			Article this_article;
//
//			/* FIXME rss 2.0 scheme */
//
//			do {
//
//				xNav.MoveToFirstChild ();
//
//				this_article = new Article ();
//				this_article.headline = xNav.Value;
//				/** title seem to be consistent **/
//
//				while (xNav.Name != "pubDate")
//				{
//					xNav.MoveToNext();
//				}
//
//				Console.WriteLine ("pubDate : " + xNav.Value);
//				xNav.MoveToParent();
//				xNav.MoveToFirstChild();
//
//				while (xNav.Name != "description")
//				{
//					xNav.MoveToNext ();
//				}
//
//				this_article.article_content = xNav.Value;
//				Console.WriteLine ("desparse xml many timeparse xml many timeparse xml many timecription: " + xNav.Value);
//				xNav.MoveToParent ();
//				xNav.MoveToFirstChild ();
//
//				while (xNav.Name != "dc:creator")
//				{
//					xNav.MoveToNext ();
//				}
//				Console.WriteLine("author: " + xNav.Value);
//				this_article.author = xNav.Value;
//
//				this_article.date = new DateTime(2013, 5, 16); /* FIXME I can't parse real value */
//				this_article.read = false;
//				this_article.score = 0;
//
//				articles.Add (this_article);
//				xNav.MoveToParent ();
//			} while(xNav.MoveToNext ());
			return 1;
		}

		private void reader_ItemAdded (object sender, System.EventArgs e)
		{
			// Add code to check if new or old article have been received
		}


	}
}